package demo;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.NameValuePair;
public class Login {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 HttpClient httpClient = new HttpClient();
	        String loginUrl = "http://weibo.com/"; //登陆url
	        try{
	            //登陆
	            PostMethod postMethod=new PostMethod(loginUrl);
	           
	            NameValuePair[] data = {
	             new NameValuePair("username","admin"), //开心网的用户名
	             new NameValuePair("password","manager"), //开心网的密码
	            };
	            postMethod.setRequestBody(data);
	           
	            int statusCode=httpClient.executeMethod(postMethod);
	            System.out.println("statusCode:"+statusCode);
	            System.out.println("HttpStatus.SC_MOVED_PERMANENTL:"+HttpStatus.SC_MOVED_PERMANENTLY);
	            System.out.println("HttpStatus.SC_MOVED_TEMPORARILY:"+HttpStatus.SC_MOVED_TEMPORARILY);
	            
	            if(statusCode==HttpStatus.SC_MOVED_PERMANENTLY||statusCode==HttpStatus.SC_MOVED_TEMPORARILY){
	              Header locationHeader = postMethod.getResponseHeader("location");
	              System.out.println("locationHeader:" + locationHeader);
	              String location = null;
	              if(locationHeader!=null){
	                location = locationHeader.getValue();
	                System.out.println("The page was redirected to:" + location);
	              }else{
	                System.err.println("Login BBS Location field value is null.");
	              }
	            }
	            postMethod.releaseConnection();
	                 
	        }catch(Exception err){
	            System.out.println(err.getMessage());
	        }finally{
	           
	        }

	}

}
